﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using cocos2d;
using CocosDenshion;


namespace CampaignWP7
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        CCApplication application;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            this.graphics.IsFullScreen = true;

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            // Extend battery life under lock.
            InactiveSleepTime = TimeSpan.FromSeconds(1);

            CCApplication application = new AppDelegate(this, graphics);
            this.Components.Add(application);
        }

        protected override void LoadContent()
        {

            base.LoadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                //
                if (GameStatusManager.instance.GetStatus() == GameStatus.GAMESTATUS_PLAY)
                {
                    //进入暂停 弹出暂停页面
                    CCDirector.sharedDirector().pushScene(GamePauseLayer.scene());
                    GameStatusManager.instance.SetStatus(GameStatus.GAMESTATUS_PAUSE);
                    //sound
                    SimpleAudioEngine.sharedEngine().pauseBackgroundMusic();

                    //sound effect
                    if (Bullet.m_iCurPlayFireEffect != -1)
                    {
                        SimpleAudioEngine.sharedEngine().stopEffect((uint)Bullet.m_iCurPlayFireEffect);
                        Bullet.m_iCurPlayFireEffect = -1;
                    }
                    if (Bullet.m_iCurPlayBalloonBombEffect != -1)
                    {
                        SimpleAudioEngine.sharedEngine().stopEffect((uint)Bullet.m_iCurPlayBalloonBombEffect);
                        Bullet.m_iCurPlayBalloonBombEffect = -1;
                    }
                    

                }
                else if (GameStatusManager.instance.GetStatus() == GameStatus.GAMESTATUS_PAUSE)
                {
                    //退出暂停
                    CCDirector.sharedDirector().popScene();
                    GameStatusManager.instance.SetStatus(GameStatus.GAMESTATUS_PLAY);
                    //sound
                    SimpleAudioEngine.sharedEngine().resumeBackgroundMusic();
                }
                else if (GameStatusManager.instance.GetStatus() == GameStatus.GAMESTATUS_TITLE)
                {
                    this.Exit();
                }
                else if (GameStatusManager.instance.GetStatus() == GameStatus.GAMESTATUS_ABOUT)
                {
                    CCDirector.sharedDirector().popScene();
                    GameStatusManager.instance.SetStatus(GameStatus.GAMESTATUS_TITLE);
                }
            }


            // TODO: Add your update logic here


            base.Update(gameTime);
        }

    }
}
